package top.tigerwoods.spring.base.core.context;

import java.util.Map;

/**
 * description  自定义上下文管理器
 *
 * @author TigerLin
 * @version 1.0
 * create 2024/06/21  00:15
 */
public class WoodsContextHandler {
    private static final ThreadLocal<WoodsContextInfo> localContext = new ThreadLocal<>();

    private WoodsContextHandler() {
    }

    /**
     * 获取当前服务域
     * @return 当前服务域名（系统号）
     */
    public static String getCurrentServiceDomain() {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getServiceDomain();
    }

    /**
     * 设置当前服务域名
     * @param serviceDomain 当前服务域名（系统号）
     */
    public static void setCurrentServiceDomain(String serviceDomain) {
        WoodsContextInfo context = getLocalContext();
        context.setServiceDomain(serviceDomain);
    }

    /**
     * 获取当前访问ID
     * @return 当前访问ID
     */
    public static String getAccessId() {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getAccessId();
    }

    /**
     * 设置当前访问ID
     * @param accessId 当前访问ID
     */
    public static void setAccessId(String accessId) {
        WoodsContextInfo context = getLocalContext();
        context.setAccessId(accessId);
    }

    /**
     * 获取当前用户
     * @return 当前用户 {@link WoodsCurrentUser}
     */
    public static WoodsCurrentUser getCurrentUser() {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getCurrentUser();
    }

    /**
     * 设置当前用户
     * @param currentUser 当前用户 {@link WoodsCurrentUser}
     */
    public static void setCurrentUser(WoodsCurrentUser currentUser) {
        WoodsContextInfo context = getLocalContext();
        context.setCurrentUser(currentUser);
    }

    /**
     * 获取单个透传属性
     * @param key 属性键名
     * @return  属性值
     */
    public static Object getTransferProperty(String key) {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getTransferProperties().get(key);
    }

    /**
     * 设置单个透传属性
     * @param key 属性键名
     * @param value 属性值
     */
    public static void setTransferProperty(String key, Object value) {
        WoodsContextInfo context = getLocalContext();
        context.getTransferProperties().put(key, value);
    }

    /**
     * 获取全部透传属性
     * @return 全部透传属性
     */
    public static Map<String, Object> getTransferProperties() {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getTransferProperties();
    }

    /**
     * 设置全部透传属性
     * @param transferProperties 全部透传属性
     */
    public static void setTransferProperties(Map<String, Object> transferProperties) {
        WoodsContextInfo context = getLocalContext();
        context.setTransferProperties(transferProperties);
    }

    /**
     * 获取单个本地属性
     * @param key 属性键名
     * @return  属性值
     */
    public static Object getLocalProperty(String key) {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getLocalProperties().get(key);
    }

    /**
     * 设置单个本地属性
     * @param key 属性键名
     * @param value 属性值
     */
    public static void setLocalProperty(String key, Object value) {
        WoodsContextInfo context = getLocalContext();
        context.getLocalProperties().put(key, value);
    }

    /**
     * 获取全部本地属性
     * @return 全部本地属性
     */
    public static Map<String, Object> getLocalProperties() {
        WoodsContextInfo context = getLocalContext();
        return context.isNull() ? null : context.getLocalProperties();
    }

    /**
     * 设置全部本地属性
     * @param localProperties 全部本地属性
     */
    public static void setLocalProperties(Map<String, Object> localProperties) {
        WoodsContextInfo context = getLocalContext();
        context.setLocalProperties(localProperties);
    }

    /**
     * 获取当前线程上下文
     * @return 当前线程上下文
     */
    private static WoodsContextInfo getLocalContext() {
        if (localContext.get() == null) {
            localContext.set(new WoodsContextInfo());
        }
        return localContext.get();
    }
}
